﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class UpdateDistanceOfMail : System.Web.UI.Page
{
    int DID;
    SqlConnection cnn;
    SqlCommand cmd;
    SqlDataReader dread;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }

        DID = Int32.Parse(Request.QueryString["DID"].ToString());
        lblDID.Text = DID.ToString();
        cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("Select * from DistanceOfMail where DistanceID = '" + DID + "'", cnn);
        dread = cmd.ExecuteReader();
        if (!IsPostBack)
        {
            if (dread.Read())
            {
                txtDName.Text = dread.GetString(1);
                txtDCharge.Text = dread.GetValue(2).ToString();
            }
        }
    }
    protected void imgbUpdate_Click(object sender, ImageClickEventArgs e)
    {
        dread.Close();
        cmd = new SqlCommand("Update DistanceOfMail Set Distance = @Distance, DistanceCharge = @DistanceCharge where DistanceID= '" + DID + "'", cnn);

        cmd.Parameters.Add(new SqlParameter("@Distance", SqlDbType.NVarChar, 50));
        cmd.Parameters["@Distance"].Value = txtDName.Text;
        cmd.Parameters.Add(new SqlParameter("@DistanceCharge", SqlDbType.Float));
        cmd.Parameters["@DistanceCharge"].Value = txtDCharge.Text;

        cmd.ExecuteNonQuery();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
    }
    protected void imgbReset_Click(object sender, ImageClickEventArgs e)
    {
        if (dread.Read())
        {
            txtDName.Text = dread.GetString(1);
            txtDCharge.Text = dread.GetValue(2).ToString();
        }
    }

    protected void imgbBack_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("DistanceOfMail_Management.aspx");
    }
}